//
//  FLMChatTextCell.m
//  FLMSuperS
//
//  Created by chenshan on 2016/10/9.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMChatTextCell.h"

@interface FLMChatTextCell ()

@property (strong, nonatomic) UIImageView *backgroundImgView;

@end

@implementation FLMChatTextCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self commitInit];
        [self buildSubviews];
    }
    return self;
}

- (CGFloat)getHeight {
    
    CGFloat result;
    
    if (self.isShowNickname) {
        result = ceil(MAX([self getContentSize].height + [self getNicknameSize].height + 2 * FLMCellPadding + 2 * FLMContentUpDownPadding + avatareFrame.size.height/2, FLMAvatarEdgeLength + 2 * FLMCellPadding));
    } else {
        result = ceil(MAX([self getContentSize].height + 2 * FLMCellPadding + 2 * FLMContentUpDownPadding + avatareFrame.size.height/2, FLMAvatarEdgeLength + 2 * FLMCellPadding));
    }
    
    return result;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    if (self.userType == FLMChatCellUserTypeMyself) {
        [self buildFrameForTextByMyself];
    }
    else {
        [self buildFrameForTextByOther];
    }
    
    [self setFrames];
    [self setImages];
}

- (void)UIPrepareForNormalStatus {
    [self.sendingIndicator stopAnimating];
    self.sendingIndicator.hidden = YES;
    self.errorImgView.hidden = YES;
}

- (void)UIPrepareForSendingDataStatus {
    self.sendingIndicator.hidden = NO;
    [self.sendingIndicator startAnimating];
    self.errorImgView.hidden = YES;
}

- (void)UIPrepareForErrorStatus {
    [self.sendingIndicator stopAnimating];
    self.sendingIndicator.hidden = YES;
    self.errorImgView.hidden = NO;
    self.errorImgView.backgroundColor = [UIColor redColor];
}

#pragma mark - private methods

- (CGSize)getContentSize {
    
    //    CGSize result = [TTTAttributedLabel sizeThatFitsAttributedString:self.contentLabel.attributedText withConstraints:CGSizeMake(FLMScreenW - FLMAvatarEdgeLength - 4 * FLMCellPadding - FLMIndicatorWidth - FLMContentLeftRightPaddingMax - FLMContentLeftRightPaddingMin, 99999) limitedToNumberOfLines:0];
    
    CGSize result = [TTTAttributedLabel sizeThatFitsAttributedString:self.contentLabel.attributedText withConstraints:CGSizeMake(200, 99999) limitedToNumberOfLines:0];
    
    float height = ceilf(MAX(result.height, FLMAvatarEdgeLength - 2 * FLMContentUpDownPadding));
    
    result = CGSizeMake(ceilf(result.width), height);
    
    return result;
}

- (void)buildFrameForTextByMyself {
    
    [self buildMyselfAvatarFrame];
    
    CGSize nicknameSize = [self buildMyselfNicknameFrame];
    
    //content frame
    CGSize contentSize =  [self getContentSize];
    contentFrame = CGRectMake(FLMScreenW - FLMAvatarEdgeLength - 2 * FLMCellPadding - FLMContentLeftRightPaddingMax - contentSize.width,
                              FLMCellPadding + nicknameSize.height + FLMContentUpDownPadding, contentSize.width, contentSize.height);
    contentFrame.origin.y = 0;
    
    //background frame
    backgroundFrame = CGRectMake(contentFrame.origin.x - FLMContentLeftRightPaddingMin,
                                 FLMCellPadding + nicknameSize.height,
                                 contentSize.width + FLMContentLeftRightPaddingMax + FLMContentLeftRightPaddingMin,
                                 contentSize.height + 2 * FLMContentUpDownPadding);
    backgroundFrame.origin.y = 0;
    //indicator frame
    sendingFrame = CGRectMake(backgroundFrame.origin.x - FLMCellPadding - FLMErrorEdgeLength, FLMCellPadding, FLMErrorEdgeLength, FLMErrorEdgeLength);
    errorFrame = sendingFrame;
}

- (void)buildFrameForTextByOther {
    
    [self buildOtherAvatarFrame];
    
    CGSize nicknameSize = [self buildOtherNicknameFrame];
    
    //content frame
    CGSize contentSize = [self getContentSize];
    contentFrame = CGRectMake(FLMAvatarEdgeLength + 2 * FLMCellPadding + FLMContentLeftRightPaddingMax,
                              FLMCellPadding + nicknameSize.height + FLMContentUpDownPadding, contentSize.width, contentSize.height);
    
    if (self.isEditing) contentFrame = CGRectMake(contentFrame.origin.x + FLMCellEditingEdgeLength + FLMCellPadding, contentFrame.origin.y, contentFrame.size.width, contentFrame.size.height);
    
    //background frame
    backgroundFrame = CGRectMake(contentFrame.origin.x - FLMContentLeftRightPaddingMax,
                                 FLMCellPadding + nicknameSize.height, contentSize.width +
                                 FLMContentLeftRightPaddingMax + FLMContentLeftRightPaddingMin,
                                 contentSize.height + 2 * FLMContentUpDownPadding);
    
    //indicator frame
    sendingFrame = CGRectMake(backgroundFrame.origin.x + backgroundFrame.size.width + FLMCellPadding, FLMCellPadding, FLMErrorEdgeLength, FLMErrorEdgeLength);
    errorFrame = sendingFrame;
    
    
}

- (void)buildSubviews {
    
    _backgroundImgView = [[UIImageView alloc] initWithFrame:CGRectZero];
    
    _contentLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
    self.contentLabel.delegate = self;
    self.contentLabel.font = FLMFontBody;
    self.contentLabel.textColor = FLMColorContent;
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.linkAttributes = @{NSForegroundColorAttributeName:FLMColorMain};
    
    [self.contentView addSubview:self.backgroundImgView];
    [self.contentView addSubview:self.contentLabel];
}

- (void)setFrames {
    
    self.avatarImgView.frame = avatareFrame;
    self.editingView.frame = editingFrame;
    self.editingView.center = CGPointMake(self.editingView.center.x, self.avatarImgView.center.y);
    self.nicknameLabel.frame = nicknameFrame;
    self.backgroundImgView.frame = backgroundFrame;
    self.contentLabel.frame = contentFrame;
    
    self.sendingIndicator.frame = sendingFrame;
    self.sendingIndicator.center = CGPointMake(self.sendingIndicator.center.x, self.backgroundImgView.center.y);
    
    self.errorImgView.frame = errorFrame;
    self.errorImgView.center = CGPointMake(self.errorImgView.center.x, self.backgroundImgView.center.y);
}

- (void)setImages {
    UIImage *coloredImage;
    if (self.userType == FLMChatCellUserTypeMyself) {
        coloredImage = [self.bubbleMyself stretchableImageWithLeftCapWidth:10 topCapHeight:30];
        self.backgroundImgView.image = coloredImage;
    }
    else {
        coloredImage = [self.bubbleOther stretchableImageWithLeftCapWidth:10 topCapHeight:30];
        self.backgroundImgView.image = coloredImage;
    }
}

- (void)setModel:(FLMMessageModel *)model {
    _model = model;
    
    self.contentLabel.text = model.content;
}

@end
